//
//  ThreeColorProgressBar.m
//  SKPAD
//
//  Created by 何睿 on 15/6/22.
//  Copyright (c) 2015年 heeeruiii. All rights reserved.
//

#import "ThreeColorProgressBar.h"

@implementation ThreeColorProgressBar

- (id) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        //初始化两个背景图
        UIImage * image = [UIImage imageNamed:@"trough(2).png"];
        _backgroundView = [[UIImageView alloc] initWithImage:image];
        _backgroundView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
        _backgroundView.backgroundColor = [UIColor clearColor];
        UIImage * image2 = [UIImage imageNamed:@"trough(2)_grey.png"];
        _backgroundView2 = [[UIImageView alloc] initWithImage:image2];
        _backgroundView2.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
        _backgroundView2.backgroundColor = [UIColor clearColor];
        
        _maxValue = 100.0;
        _minValue = 0.0;
        _realValue = 0.0;
        //画刻度层
        [self paintingScale];
        [self initRealValueTrack];
        [self.layer insertSublayer:_backgroundView2.layer atIndex:0];
        [self.layer insertSublayer:_backgroundView.layer atIndex:3];
        [self.layer insertSublayer:_layerScale atIndex:1];
        [self.layer insertSublayer:_layerReal atIndex:2];
    }
    return  self;
}

-(void) paintingScale
{
    //初始化图层
    _layerScale = [CALayer layer];
    _layerScale.frame = self.bounds;
    _layerScale.position = CGPointMake(CGRectGetWidth(self.frame)/2.0, CGRectGetHeight(self.frame)/2.0);
    //初始化刻度层
    _trackLayerScale = [CAShapeLayer layer];
    _trackLayerScale.frame = self.bounds;
    _trackLayerScale.position = CGPointMake(CGRectGetWidth(self.frame)/2.0, CGRectGetHeight(self.frame)/2.0);
    _trackLayerScale.fillColor = [UIColor clearColor].CGColor;
    _trackLayerScale.strokeColor = [[UIColor blueColor] CGColor];
    _trackLayerScale.lineCap = kCALineCapRound;
    
    //初始化颜色层
    //绿色
    _gradientLayerGreen = [CAGradientLayer layer];
    _gradientLayerGreen.frame = CGRectMake(0, self.bounds.size.height * 0.545, self.bounds.size.width, self.bounds.size.height * 0.455);
    //图层中心，根据整个空间坐标系定位
    _gradientLayerGreen.position = CGPointMake(CGRectGetWidth(self.frame)/2.0, (CGRectGetHeight(self.frame) + self.bounds.size.height * 0.545)/2.0);
    //黄色
    _gradientLayerYellow = [CAGradientLayer layer];
    _gradientLayerYellow.frame = CGRectMake(0, self.bounds.size.height * 0.045, self.bounds.size.width, self.bounds.size.height * 0.5);
    _gradientLayerYellow.position = CGPointMake(CGRectGetWidth(self.frame)/2.0, (CGRectGetHeight(self.frame) - self.bounds.size.height * 0.5)/2.0 + self.bounds.size.height * 0.045);
    //红色
    _gradientLayerRed = [CAGradientLayer layer];
    _gradientLayerRed.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height * 0.045);
    _gradientLayerRed.position = CGPointMake(CGRectGetWidth(self.frame)/2.0, (CGRectGetHeight(self.frame) - self.bounds.size.height * 0.955)/2.0);
    
    CAKeyframeAnimation *animation = [CAKeyframeAnimation
                                      animationWithKeyPath:@"transform.rotation.z"];
    animation.duration = 0.25f;
    animation.keyTimes = @[@(0), @(0.5), @(1.0)];
    animation.timingFunction = [CAMediaTimingFunction
                                functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.trackLayerScale addAnimation:animation forKey:nil];
    
    UIBezierPath * lineTrajectory = [UIBezierPath bezierPath];
    CGFloat eachValue = self.frame.size.height / 67.5;
    _trackLayerScale.lineWidth = eachValue;
    CGFloat startY = self.frame.size.height - eachValue * 2;
    for (int i = 0; i < 22; ++i)
    {
        [lineTrajectory moveToPoint:CGPointMake(0, startY)];
        [lineTrajectory addLineToPoint:CGPointMake(_trackLayerScale.frame.size.width, startY)];
        startY -= eachValue * 3;
    }
    
    //连线
    _trackLayerScale.path = lineTrajectory.CGPath;
    _trackLayerScale.strokeStart = 0.f;
    _trackLayerScale.strokeEnd = 1.f;
    UIColor * darkGreen = (id)[[UIColor colorWithRed:38.0/255 green:90.0/255 blue:43.0/255 alpha:1] CGColor];
    UIColor * darkYellow = (id)[[UIColor colorWithRed:86.0/255 green:88.0/255 blue:42.0/255 alpha:1] CGColor];
    UIColor * darkRed = (id)[[UIColor colorWithRed:56.0/255 green:0.0/255 blue:0.0/255 alpha:1] CGColor];
    
    [_gradientLayerGreen setColors:[NSArray arrayWithObjects:darkGreen,darkGreen, nil]];
    [_gradientLayerGreen setLocations:@[@0.0,@1.0]];
    [_gradientLayerGreen setStartPoint:CGPointMake(0.5, 1)];
    [_gradientLayerGreen setEndPoint:CGPointMake(0.5, 0)];
    
    [_gradientLayerYellow setColors:[NSArray arrayWithObjects:darkYellow,darkYellow, nil]];
    [_gradientLayerYellow setLocations:@[@0.0,@1.0]];
    [_gradientLayerYellow setStartPoint:CGPointMake(0.5, 1)];
    [_gradientLayerYellow setEndPoint:CGPointMake(0.5, 0)];
    
    [_gradientLayerRed setColors:[NSArray arrayWithObjects:darkRed,darkRed, nil]];
    [_gradientLayerRed setLocations:@[@0.0,@1.0]];
    [_gradientLayerRed setStartPoint:CGPointMake(0.5, 1)];
    [_gradientLayerRed setEndPoint:CGPointMake(0.5, 0)];
    
    [_layerScale addSublayer:_gradientLayerGreen];
    [_layerScale addSublayer:_gradientLayerYellow];
    [_layerScale addSublayer:_gradientLayerRed];
    [_layerScale setMask:_trackLayerScale];
}

- (void) initRealValueTrack
{
    //初始化图层
    _layerReal = [CALayer layer];
    _layerReal.frame = self.bounds;
    _layerReal.position = CGPointMake(CGRectGetWidth(self.frame)/2.0, CGRectGetHeight(self.frame)/2.0);
    //初始化刻度层
    _trackLayerReal = [CAShapeLayer layer];
    _trackLayerReal.frame = self.bounds;
    _trackLayerReal.position = CGPointMake(CGRectGetWidth(self.frame)/2.0, CGRectGetHeight(self.frame)/2.0);
    _trackLayerReal.fillColor = [UIColor clearColor].CGColor;
    _trackLayerReal.strokeColor = [[UIColor blueColor] CGColor];
    _trackLayerReal.lineCap = kCALineCapRound;
    _trackLayerReal.strokeStart = 0.f;
    _trackLayerReal.strokeEnd = 1.f;
    
    //初始化颜色层
    UIColor * greenColor = (id)[[UIColor greenColor] CGColor];
    UIColor * yellowColor = (id)[[UIColor yellowColor] CGColor];
    UIColor * redColor = (id)[[UIColor redColor] CGColor];

    //绿色
    _realLayerGreen = [CAGradientLayer layer];
    _realLayerGreen.frame = CGRectMake(0, self.bounds.size.height * 0.545, self.bounds.size.width, self.bounds.size.height * 0.455);
    [_realLayerGreen setColors:[NSArray arrayWithObjects:greenColor, greenColor, nil]];
    [_realLayerGreen setLocations:@[@0.0,@1.0]];
    [_realLayerGreen setStartPoint:CGPointMake(0.5, 1)];
    [_realLayerGreen setEndPoint:CGPointMake(0.5, 0)];
    //图层中心，根据整个空间坐标系定位
    _realLayerGreen.position = CGPointMake(CGRectGetWidth(self.frame)/2.0, (CGRectGetHeight(self.frame) + self.bounds.size.height * 0.545)/2.0);
    
    //黄色
    _realLayerYellow = [CAGradientLayer layer];
    _realLayerYellow.frame = CGRectMake(0, self.bounds.size.height * 0.045, self.bounds.size.width, self.bounds.size.height * 0.5);
    _realLayerYellow.position = CGPointMake(CGRectGetWidth(self.frame)/2.0, (CGRectGetHeight(self.frame) - self.bounds.size.height * 0.5)/2.0 + self.bounds.size.height * 0.045);
    [_realLayerYellow setColors:[NSArray arrayWithObjects:yellowColor, yellowColor, nil]];
    [_realLayerYellow setLocations:@[@0.0,@1.0]];
    [_realLayerYellow setStartPoint:CGPointMake(0.5, 1)];
    [_realLayerYellow setEndPoint:CGPointMake(0.5, 0)];
    //红色
    _realLayerRed = [CAGradientLayer layer];
    _realLayerRed.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height * 0.045);
    _realLayerRed.position = CGPointMake(CGRectGetWidth(self.frame)/2.0, (CGRectGetHeight(self.frame) - self.bounds.size.height * 0.955)/2.0);
    [_realLayerRed setColors:[NSArray arrayWithObjects:redColor, redColor, nil]];
    [_realLayerRed setLocations:@[@0.0,@1.0]];
    [_realLayerRed setStartPoint:CGPointMake(0.5, 1)];
    [_realLayerRed setEndPoint:CGPointMake(0.5, 0)];
    [_layerReal addSublayer:_realLayerGreen];
    [_layerReal addSublayer:_realLayerYellow];
    [_layerReal addSublayer:_realLayerRed];
    [_layerReal setMask:_trackLayerReal];

}

- (void)setMyRealValue:(CGFloat)realValue
{
    if (_realValue != realValue)
    {
        _realValue = realValue;
        int tempValue = (int)((realValue - _minValue)/(_maxValue - _minValue) * 22);
        UIBezierPath * lineTrajectory = [UIBezierPath bezierPath];
        CGFloat eachValue = self.frame.size.height / 67.5;
        _trackLayerReal.lineWidth = eachValue;
        CGFloat startY = self.frame.size.height - eachValue * 2;
        for (int i = 0; i < tempValue; ++i)
        {
            [lineTrajectory moveToPoint:CGPointMake(0, startY)];
            [lineTrajectory addLineToPoint:CGPointMake(_trackLayerReal.frame.size.width, startY)];
            startY -= eachValue * 3;
        }
        //连线
        _trackLayerReal.path = lineTrajectory.CGPath;

    }
}
@end
